#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogabout.h"

#include <QCloseEvent>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->mainToolBar->hide();
    // add a form and set layout
    QWidget *pWidgetProfile =  ui->tabWidget->widget(0);
    m_pFormDeviceProfile = new FormDeviceProfile(&m_Profile, pWidgetProfile);
    pWidgetProfile->layout()->addWidget(m_pFormDeviceProfile);

    QWidget *pWidgetMixer =  ui->tabWidget->widget(1);
    m_pFormAlbumMixer = new FormAlbumMixer(&m_Profile, pWidgetMixer);
    pWidgetMixer->layout()->addWidget(m_pFormAlbumMixer);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_actionAbout_triggered()
{
    DialogAbout dialogAbout;
    dialogAbout.exec();
}

void MainWindow::on_actionDelete_triggered()
{
    m_pFormAlbumMixer->actionDelete();
}

void MainWindow::on_tabWidget_currentChanged(int index)
{
    if(index != 0)
    {
        m_pFormDeviceProfile->Save2Profile();
        m_pFormAlbumMixer->doReshape();
    }

    if(index == 1)
        ui->actionDelete->setEnabled(true);
    else
        ui->actionDelete->setEnabled(false);

}

void MainWindow::on_actionSave_triggered()
{
    m_pFormAlbumMixer->actionSave();
}

void MainWindow::on_actionLoad_triggered()
{
    m_pFormAlbumMixer->actionLoad();
}

void MainWindow::on_actionAbortProcessing_triggered()
{
    m_pFormAlbumMixer->actionAbortProcessing();
}

void MainWindow::closeEvent(QCloseEvent *event)
{
    m_pFormDeviceProfile->SaveData();
    if(!m_pFormAlbumMixer->isModified())
    {
        event->accept();
        return;
    }

    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int nRet = msgBox.exec();

    switch (nRet)
    {
    case QMessageBox::Save:
        // Save was clicked
        if(m_pFormAlbumMixer->actionSave())
            event->accept();
        else
            event->ignore();
        break;
    case QMessageBox::Discard:
        // Don't Save was clicked
        event->accept();
        break;
    case QMessageBox::Cancel:
        // Cancel was clicked
        event->ignore();
        break;
    default:
        event->ignore();
        break;
    }
}
